package co.yixiang.modules.product.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.product.domain.YxBusinessType;
import co.yixiang.modules.product.service.YxBusinessTypeService;
import co.yixiang.modules.product.service.dto.YxBusinessTypeDto;
import co.yixiang.modules.product.service.dto.YxBusinessTypeQueryCriteria;
import co.yixiang.modules.product.service.mapper.YxBusinessTypeMapper;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2021-06-21
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxBusinessType")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxBusinessTypeServiceImpl extends BaseServiceImpl<YxBusinessTypeMapper, YxBusinessType> implements YxBusinessTypeService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<YxBusinessTypeDto> queryAll(YxBusinessTypeQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxBusinessType> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,YxBusinessTypeDto.class);
    }


    @Override
    //@Cacheable
    public List<YxBusinessType> queryAll(YxBusinessTypeQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxBusinessType.class, criteria));
    }


    @Override
    public void download(List<YxBusinessTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxBusinessTypeDto yxBusinessType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("父id", yxBusinessType.getPid());
            map.put("分类名称", yxBusinessType.getBusinessName());
            map.put("排序", yxBusinessType.getSort());
            map.put("图标", yxBusinessType.getPic());
            map.put("是否推荐", yxBusinessType.getIsShow());
            map.put("添加时间", yxBusinessType.getCreateTime());
            map.put(" updateTime",  yxBusinessType.getUpdateTime());
            map.put("删除状态", yxBusinessType.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
